Repository navigation
Fix unary minus directly after ^ in the bundled Javaluator - #198
Merged
Merged
Conversation
A prefix operator has no left operand, so it must never pop a pending operator off the stack. The shunting-yard loop let unary minus (precedence 3) pop a pending ^ (precedence 4), which then ran before its right operand was read: `x * 2^-1` returned -900 for x=30 and a bare `2^-1` was rejected. Skip the pop loop for unary operators. Both the string and the compiled evaluator share this parser. The existing test that pinned the rejection of `2^-1` now checks that the compiled and string evaluators agree. Fixes #197 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FaEyTsM3SWu5hP2sUDjCjR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #197
Problem
A unary minus directly after
^gave a wrong number when another operator came before it. With x=30,x * 2^-1returned -900 instead of 15, and a bare2^-1was rejected. Both the string evaluator (Evaluator.eval/DoubleEvaluator.evaluate) and the compiled one (Evaluator.compile) were affected, because they share one parser.Root cause
In
AbstractEvaluator.evaluate, the shunting-yard loop for an incoming operator pops pending operators of higher precedence. A unary minus (precedence 3) therefore popped a pending^(precedence 4) before the^had its right operand, so^consumed the wrong values (or too few).Fix
A prefix operator has no left operand and must never pop anything, so the pop loop is skipped for unary operators. This is a one-line change. Every other expression takes the same path as before. Unary minus still binds looser than
^, so-2^2is still -4 and2^-3^2is2^-(3^2). Excel style (NEGATE_HIGH) is unchanged.Tests
NegativeExponentTestruns the same cases through both evaluators:2^-1,x * 2^-1,2^-x,-2^2,2^--1,2^-3^2, inside functions and brackets, and the Excel style. It failed before the fix, with the issue's -900, and passes now.CompiledExpressionTesthad a test that pinned2^-1,-d^-xandd^-1as rejected. The issue prefers correct evaluation, so it now checks that the compiled and string evaluators agree on those expressions.codex-apisuite: 407 tests, 0 failures.Downstream
magemonkeystudio/divinity has a guard for this bug (
NEGATIVE_EXPONENTinCombatFormulas). It can be removed once this ships, along with raising itsMIN_CORE_VERSIONto the Codex build that has the fix.Generated by Claude Code